package com.itheima.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 短信发送工具类
 */

public class SMSUtils {

    /**
     * 发送短信
     * @param signName 签名
     * @param templateCode 模板
     * @param PhoneNumbers 手机号
     * @param templateParam 参数
     */
    public static void SendMag(String signName,String templateCode,String PhoneNumbers,String templateParam) throws IOException {

//        BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\86176\\Desktop\\AccessKey.txt")));
//        String accessKeyId = br.readLine();
//        String accessKeySecret = br.readLine();

        String accessKeyId = "LTAI5tGeYt8Kg1NnHZKBL344";
        String accessKeySecret = "rARcWrcnCB2hKXrEW6XgwB1LnSn0h6";
        DefaultProfile profile = DefaultProfile.getProfile("cn-nanjing",
                accessKeyId,
                accessKeySecret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/

        IAcsClient client = new DefaultAcsClient(profile);


        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setPhoneNumbers(PhoneNumbers);
        request.setTemplateParam("{\"code\":\"" +
                templateParam +
                "\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            System.out.println("短信发送成功");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }

}
